package com.wyt.common.httpcom;

import com.wyt.common.httpcom.bean.TwoInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * @ClassName RestTemplateHttp
 * @Description @TODO
 * @Author wangyongtao
 * @Date 2019/12/30 21:30
 * @Version 1.0
 */

public class RestTemplateDemo {

    private static Logger logger  = LoggerFactory.getLogger(RestTemplateDemo.class);
    private RestTemplate restTemplate = new RestTemplate();

    public RestTemplateDemo() {
        this.restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
    }

    /**
     * post请求
     */
    public String doPostByTemplate(String url, String request) {
        //1、请求头
        HttpHeaders headers = new HttpHeaders();
//        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
//        headers.setContentType(type);
        headers.add("Content-Type","application/json;charset=utf-8");
        headers.add("connection","Keep-Alive");
        headers.add("accept", "*/*");
//        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        //4、用json字符串请求进行调用
        String str = null;
        HttpEntity<String> httpEntity = null;
        try {
            httpEntity = new HttpEntity<>(request,headers);
            str = restTemplate.postForObject(url,httpEntity,String.class);
            logger.info("用json字符串请求进行调用的结果是：[{}]",str);
        } catch (Exception e) {
            logger.error("用json字符串请求进行调用进入异常，异常信息为：",e);
        }
        return str;
    }

    /**
     * post请求(再请求头中添加session_id)
     */
    public String doPostHeaderByTemplate(String url, String request,String session_id) {
        //1、请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("session_id",session_id);
        //4、用json字符串请求进行调用
        String str = null;
        HttpEntity<String> httpEntity = null;
        try {
            httpEntity = new HttpEntity<>(request,headers);
            str = restTemplate.postForObject(url,httpEntity,String.class);
            logger.info("用带请求头的json字符串请求进行调用的结果是：[{}]",str);
        } catch (Exception e) {
            logger.error("用带请求头的json字符串请求进行调用进入异常，异常信息为：",e);
        }
        return str;
    }

    /**
      * @Description: restTemplate链接参数设置
      * @Author: wangyongtao
      **/
    public void testUpdateRestTemplateParam(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(15000);
        factory.setReadTimeout(5000);
        RestTemplate restTemplate = new RestTemplate(factory);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:17001/springbdemo/visit/getString", String.class);
        System.out.println(responseEntity.getBody());
        String str = restTemplate.postForObject("http://localhost:17001/springbdemo/visit/comprosizeBody",new TwoInfo("123","456"),String.class);
        System.out.println(str);
    }



    public static void main(String[] args) {

    }
}
